package com.handpay.nocardpay.baselibrary.pay.qrcodepay.presenter;

import android.content.Context;

import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.presenter.IBasePresenter;
import com.handpay.nocardpay.baselibrary.pay.qrcodepay.model.IGetQrCodeModel;
import com.handpay.nocardpay.baselibrary.pay.qrcodepay.model.bean.QrcodeRspBean;
import com.handpay.nocardpay.baselibrary.pay.qrcodepay.model.impl.GetQrCodeModelImpl;
import com.handpay.nocardpay.baselibrary.pay.qrcodepay.view.IQrcodePayView;

import java.util.HashMap;

/**
 * Created by sxshi on 2017-10-12.
 */

public class QrCodePresenter implements IBasePresenter {

    private Context mContext;
    private IQrcodePayView qrcodePayView;

    private IGetQrCodeModel<QrcodeRspBean> getQrCodeModel;


    public IQrcodePayView getQrcodePayView() {
        return qrcodePayView;
    }

    public void setQrcodePayView(IQrcodePayView qrcodePayView) {
        this.qrcodePayView = qrcodePayView;
    }

    public QrCodePresenter(Context mContext) {
        this.mContext = mContext;
        getQrCodeModel = new GetQrCodeModelImpl(mContext);
    }

    public void getQrcodeUrl(HashMap<String, Object> paramers, boolean isLoading) {
        if (isLoading) {
            if (qrcodePayView != null) {
                qrcodePayView.showProgress();
            }
        }
        getQrCodeModel.getQrcode(QrcodeRspBean.class, paramers, new IBaseModel.OnRequestFinishedListener<QrcodeRspBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                if (qrcodePayView != null) {
                    qrcodePayView.hideProgress();
                    qrcodePayView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(QrcodeRspBean response) {
                if (qrcodePayView != null) {
                    qrcodePayView.hideProgress();
                    qrcodePayView.getQrCodeResult(response);
                }
            }
        });
    }

    @Override
    public void onDestory() {
        if (qrcodePayView != null) {
            qrcodePayView = null;
        }
    }
}
